package com.example.toolbar.fragment;

import com.ab.http.AbRequestParams;
import com.example.toolbar.R;
import com.example.toolbar.widget.PagerSlidingTabStrip;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class fragmentNews extends Fragment {
	private PagerSlidingTabStrip mPagerSlidingTabStrip;
	private ViewPager mViewPager;
	private Fragment[] fragments = new Fragment[8];
	private AbRequestParams params;
	private String url="http://tr.zzapi.gson.cn/api/news/list?";
	private String[] type=new String[]{
			"HOT","YL","SZ","LS","HOT","IMPORTANT","MS","WHT"
	};

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragmenttest, null);
		initFragment();
		initeView(view);
		initTabsValue();
		return view;

	}



	public void initeView(View view) {
		mPagerSlidingTabStrip = (PagerSlidingTabStrip) view
				.findViewById(R.id.tabs);
		mViewPager = (ViewPager) view.findViewById(R.id.pager);
		mViewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
		mPagerSlidingTabStrip.setViewPager(mViewPager);
		mPagerSlidingTabStrip
				.setOnPageChangeListener(new OnPageChangeListener() {

					@Override
					public void onPageSelected(int arg0) {
						// colorChange(arg0);
					}

					@Override
					public void onPageScrolled(int arg0, float arg1, int arg2) {
					}

					@Override
					public void onPageScrollStateChanged(int arg0) {
					}
				});
	}

	private void initTabsValue() {
		mPagerSlidingTabStrip.setIndicatorColor(Color.parseColor("#4876FF"));
		mPagerSlidingTabStrip.setDividerColor(Color.TRANSPARENT);
		mPagerSlidingTabStrip.setBackgroundColor(Color.parseColor("#699bf9"));
		mPagerSlidingTabStrip.setUnderlineHeight((int) TypedValue
				.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources()
						.getDisplayMetrics()));
		mPagerSlidingTabStrip.setIndicatorHeight((int) TypedValue
				.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources()
						.getDisplayMetrics()));
		mPagerSlidingTabStrip.setSelectedTextColor(Color.WHITE);
		mPagerSlidingTabStrip.setTextColor(Color.BLACK);
	}

	public class MyPagerAdapter extends FragmentPagerAdapter {

		private final String[] TITLES = { "新闻专题", "时尚娱乐", "株洲时政", "军事历史",
				"热门", "要闻", "株洲民生", "微话题" };

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return TITLES[position];
		}

		@Override
		public int getCount() {
			return TITLES.length;
		}

		@Override
		public Fragment getItem(int position) {
			return fragments[position];
		}

	}

	private void initFragment() {
		for (int i = 0; i < fragments.length; i++) {
			params=new AbRequestParams();
			params.put("type", type[i]);
			fragments[i] = new FragmentLoad(params,url,true,true,false);
		}
		fragments[0]=new fragmentsubject();
	}
}
